package Controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import Boundry.ApproveDenyExtenstionGUI;
import Controllers.GenericToolsForController.RequestMainStatusEnum;
import Controllers.GenericToolsForController.RequestStepStatusEnum;
import Controllers.GenericToolsForController.WorkerPostion;
import Entity.Message;
import Entity.Request;
import Entity.RequestArray;
import Entity.Status;
import Entity.User;
import Entity.Worker;

public class RequestManager 
{
	
	/*
	 * Singleton 
	 */
	private static  RequestManager Instance = null;
	private RequestManager(){}
	public static RequestManager getInstance()
	{
		if(Instance == null)
		{
			Instance = new RequestManager();
		}
		return Instance;
	}
	
	
	public List<Request> getAllRequestToCloseRequest(int inspectorid)
	{
		int inspectorId;
		if(inspectorid <= 0)
			inspectorId = GlobalData.worker.getId();
		else inspectorId = inspectorid;
		Status[] stats= new Status[2];
		stats[0] = new Status(RequestStepStatusEnum.REV_REVIEWED_CLOSE, RequestMainStatusEnum.OPEN);
		stats[1] = new Status(RequestStepStatusEnum.TST_PASSED, RequestMainStatusEnum.OPEN);

		return GenricGetAllRequestByStatusAndPostion(inspectorId,WorkerPostion.INSPECTOR,stats);
		
		/*
		int inspectorId = GlobalData.user.getUserId();
		
		//The Request must be open
		String mainStatus = RequestMainStatusEnum.OPEN.getText();
		
		//The Request can be close from the Review And Decision step
		String stepStatus1 = RequestStepStatusEnum.REV_REVIEWED_CLOSE.getText();
		//The Request close after Testing step
		String stepStatus2 = RequestStepStatusEnum.TST_PASSED.getText();
		
		String[] data = new String[]{inspectorId+"",mainStatus,stepStatus1,stepStatus2};
		
		Message msg = new Message(7001, GlobalData.MESSAGE_ID(),data);
		Message AnswerMessage = null;
		
		try 
		{
			GlobalData.client.sendToServer(msg);
			while (true)
			{
				if (GlobalData.client.isRecivedMessage()==true)
				{
					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
				    if (AnswerMessage!=null)
				  	      break;
				}	
			}
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if ( AnswerMessage==null || AnswerMessage.getDataGram() == null)
		{
			// for this user There is not Request to be Close
			return null;
		}
		else
		{
			if(AnswerMessage.getDataGram() instanceof List<?> )
				   return	(List<Request>)AnswerMessage.getDataGram();
		}	
		return null;*/
	}
	
	public  List<Request> getAllRequestThatCanBeSuspend()
	{
		String inspectorId = ((Integer)GlobalData.worker.getId()).toString();
		//The Request must be open
		String mainStatus = RequestMainStatusEnum.OPEN.getText();

		String[] data = new String[]{inspectorId,mainStatus};
		
		Message msg = new Message(7002, GlobalData.MESSAGE_ID(),data);
		Message AnswerMessage = null;
		
		try 
		{
			GlobalData.client.sendToServer(msg);
			while (true)
			{
				if (GlobalData.client.isRecivedMessage()==true)
				{
					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
				    if (AnswerMessage!=null)
				  	      break;
				}	
			}
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		
		
		if ( AnswerMessage==null || AnswerMessage.getDataGram() == null)
		{
			// for this user There is not Request to be Close
			return null;
		}
		else
		{
			if(AnswerMessage.getDataGram() instanceof List<?> )
				   return	(List<Request>)AnswerMessage.getDataGram();
		}
		
		return null;
		
	}
	
	/**
	 * @author Shimon Arzuan
	 * return list of all request that are suspended.
	 * @return List<Request>
	 */
	public List<Request> getAllSuspendedRequests()
	{

		String mainStatus = RequestMainStatusEnum.SUSPEND.getText();

		String[] data = new String[]{mainStatus};
		
		Message msg = new Message(7004, GlobalData.MESSAGE_ID(),data);
		Message AnswerMessage = null;
		
		try 
		{
			GlobalData.client.sendToServer(msg);
			while (true)
			{
				if (GlobalData.client.isRecivedMessage()==true)
				{
					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
				    if (AnswerMessage!=null)
				  	      break;
				}	
			}
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		
		
		if ( AnswerMessage==null || AnswerMessage.getDataGram() == null)
		{
			// for this user There is not Request to be Close
			return null;
		}
		else
		{
			if(AnswerMessage.getDataGram() instanceof List<?> )
				   return	(List<Request>)AnswerMessage.getDataGram();
		}
		
		return null;
	}
	
	
	public List<Request> getAllCloseRequests()
	{

		String mainStatus = RequestMainStatusEnum.CLOSE.getText();

		String[] data = new String[]{mainStatus};
		
		Message msg = new Message(7005, GlobalData.MESSAGE_ID(),data);
		Message AnswerMessage = null;
		
		try 
		{
			GlobalData.client.sendToServer(msg);
			while (true)
			{
				if (GlobalData.client.isRecivedMessage()==true)
				{
					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
				    if (AnswerMessage!=null)
				  	      break;
				}	
			}
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
			
		if ( AnswerMessage==null || AnswerMessage.getDataGram() == null)
		{
			// for this user There is not Request to be Close
			return null;
		}
		else
		{
			System.out.println("Return list of Close Req");
			if(AnswerMessage.getDataGram() instanceof List<?> )
				   return	(List<Request>)AnswerMessage.getDataGram();
		}
		
		return null;
	} 
	
	@SuppressWarnings("unchecked")
	public List<Request> getAllOpenRequests()
	{

		String mainStatus = RequestMainStatusEnum.OPEN.getText();

		String[] data = new String[]{mainStatus};
		
		Message msg = new Message(7009, GlobalData.MESSAGE_ID(),data);
		Message AnswerMessage = null;
		System.out.println("Enter to Open Req");
		try 
		{
			GlobalData.client.sendToServer(msg);
			while (true)
			{
				if (GlobalData.client.isRecivedMessage()==true)
				{
					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
				    if (AnswerMessage!=null)
				  	      break;
				}	
			}
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		if ( AnswerMessage==null || AnswerMessage.getDataGram() == null)
		{
			// for this user There is not Request to be Close
			return null;
		}
		else
		{
			System.out.println("Return Open Req");
			if(AnswerMessage.getDataGram() instanceof List<?> )
				   return	(List<Request>)AnswerMessage.getDataGram();
		}
		return null;
	} 
	
	
	
	
	
	
	public Request convertToRequest(String[] data)
	{
		return null;
	}
	
	public Request[] convertToRequestArray(String[][] data)
	{
		if (data != null) {
			Request[] list = new Request[data.length];
			for (int i = 0; i < data.length; i++) {
				list[i] = convertToRequest(data[i]);
			}
			return list;
		}
		return null;
	}
	
	
	
	
	
	
	
	
	
	
	public boolean createRequest(Request req){
		return false;}
	public boolean removeRequest(int ireqID){
		return false;} 
	public List<Request> getAllRequests() {
	
		
		return null;
		
		
		
		
	}
	public Request getRequestByID(int ReqID) 
	{
		Message msg = new Message(7006, GlobalData.MESSAGE_ID(),ReqID);
		Message AnswerMessage = null;
		
		try 
		{
			GlobalData.client.sendToServer(msg);
			while (true)
			{
				if (GlobalData.client.isRecivedMessage()==true)
				{
					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
				    if (AnswerMessage!=null)
				  	      break;
				}	
			}
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (AnswerMessage.getDataGram()==null)
      		 return null;
		if(AnswerMessage.getDataGram() instanceof Request )
				return (Request) AnswerMessage.getDataGram();
		return null;
	}
	
	public ArrayList<Request> getUnNominatedRequests(){
		Message msg = new Message(7007, GlobalData.MESSAGE_ID(),null);
		Message AnswerMessage = null;
		
		try 
		{
			GlobalData.client.sendToServer(msg);
			while (true)
			{
				if (GlobalData.client.isRecivedMessage()==true)
				{
					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
				    if (AnswerMessage!=null)
				  	      break;
				}	
			}
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
		return (ArrayList<Request>) AnswerMessage.getDataGram();
		
	}
	
	
	
	
	public Request[] getNominatedOpenRequests(){
		return null;}
	
	
	
	public List<Request> getAllUnassignedRequest(int id, WorkerPostion pos) {
	
		Message AnswerMessage = null;
		Object[] objarr = {new Integer(id),pos};
		Message msg = new Message(7010, GlobalData.MESSAGE_ID(),objarr);
		try 
		{
			GlobalData.client.sendToServer(msg);
			while (true)
			{
				if (GlobalData.client.isRecivedMessage()==true)
				{
					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
				    if (AnswerMessage!=null)
				  	      break;
				}	
			}
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
		return (List<Request>) AnswerMessage.getDataGram();
		
		
		
		
		}
	
	/*
	 * TODO review that no one uses these
	Ron: I do not use these after all. if no one need them they can be removed.
	public Request[] getAllRequestsUnevaluatedWithoutTime(Worker eval ) {
		return null;}
	public Request[] getAllRequestsUnevaluatedWithTime(EvaluatorController eval) 	{
		return null;}		
	public Request[] getAllRequestsForReevaluation(EvaluatorController eval) {
		return null;}					
		
		
	*/
	public int getNumberOfSuspendedRequests() {
		return 0;}					
	public int getNumberOfClosedRequests() {
		return 0;}
	public int getNumberOfOpenRequests() {
		return 0;}					
	public int getNumberOfRejectedRequests() {
		return 0;}	
	
	
	@SuppressWarnings("unchecked")
	public List<Request> getAllRequestsToBeReviewed(Worker wrk) 
	
	{	

		int CHAIRMEN = GlobalData.worker.getId();

		Status[] stats= new Status[4];
		stats[0] = new Status(RequestStepStatusEnum.REV_REVIEWED_EXTENDED, RequestMainStatusEnum.OPEN);
		stats[1] = new Status(RequestStepStatusEnum.REV_REVIEWED_TIME_EXTENSION_PENDING, RequestMainStatusEnum.OPEN);
		stats[2] = new Status(RequestStepStatusEnum.REV_REVIEWED_EXTENDED_DENY, RequestMainStatusEnum.OPEN);
		stats[3] = new Status(RequestStepStatusEnum.EVL_PASSED, RequestMainStatusEnum.OPEN);

		return GenricGetAllRequestByStatusAndPostion(CHAIRMEN,WorkerPostion.CHAIR_MEN,stats);
		/*
        if (wrk instanceof Worker)
        {
       		Message msg = new Message(7017, GlobalData.MESSAGE_ID(),wrk);
       		Message AnswerMessage = null;
       		
       		try 
       		{
       			GlobalData.client.sendToServer(msg);
       			while (true)
       			{
       				if (GlobalData.client.isRecivedMessage()==true)
       				{
       					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
       				    if (AnswerMessage!=null)
       				  	      break;
       				}	
       			}
       		} 
       		catch (IOException e) 
       		{
       			// TODO Auto-generated catch block
       			e.printStackTrace();
       		}
       		
       		
       		if (AnswerMessage.getDataGram() instanceof List<?> && ((( List<?>)AnswerMessage.getDataGram()).isEmpty() == false) && ((( List<?>)AnswerMessage.getDataGram()).get(0) instanceof Request) ) 
	       		{
       			System.out.println("\nReturn answer msg\n");
	       		return (List<Request>) AnswerMessage.getDataGram();
	       		}
       		else
       			{
       			System.out.println("\nReturn null msg\n");
       			return null;
       			}
       	
        }
		return null;
		*/
	}
	
	
	public Request[] getAllRequestsToBeExecutaed(ExecutionLeaderController exe) {
		return null;}			
	
	
	public List<Request> getAllRequestsWithoutExaminer(int id,WorkerPostion pos , Status[] stats) 
	{
		
		List<Request>  reqs= this.GenricGetAllRequestByStatusAndPostion(id,pos,stats);
		List<Request> removeObj = new ArrayList<Request>();
		for (int i= 0;i<reqs.size();i++)
		{
			if (reqs.get(i).getExaminer() !=null)
				removeObj.add(reqs.get(i));
		}
		for (Request req : removeObj)
		{
			reqs.remove(req);
		}
		
		return reqs;
		
	
	}
	
	public List<Request> getAllRequestToBeTested(int id,WorkerPostion pos,Status[] stats) {
		
			System.out.println("Worker id is :"+id);
			return (GenricGetAllRequestByStatusAndPostion(id,pos,stats));
	
	
	}
	
	
	public boolean UpdateAfterTestingFailed(int reqId, Status newStatus,String commentToHistory, int dayOfStep) {
		
		
		Message AnswerMessage = null;
		Object [] objArr=new Object[4];
		objArr[0]=reqId;
		objArr[1]=newStatus;
		objArr[2]=commentToHistory;
		objArr[3]=dayOfStep;
		
		
	       		Message msg = new Message(7028, GlobalData.MESSAGE_ID(),objArr);
	       		
	       		try 
	       		{
	       			GlobalData.client.sendToServer(msg);
	       			while (true)
	       			{
	       				if (GlobalData.client.isRecivedMessage()==true)
	       				{
	       					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
	       				    if (AnswerMessage!=null)
	       				  	      break;
	       				}	
	       			}
	       		} 
	       		catch (IOException e) 
	       		{
	       			// TODO Auto-generated catch block
	       			e.printStackTrace();
	       		}
	       	
	        
		 if (AnswerMessage!=null)
       		 return true;
       	 else 
       		 return false;

}
	
	

	public boolean UpdateAfterTestingPassed(int reqId, Status newStatus,String commentToHistory, int dayOfStep) {

		Message AnswerMessage = null;
		Object [] objArr=new Object[4];
		objArr[0]=reqId;
		objArr[1]=newStatus;
		objArr[2]=commentToHistory;
		objArr[3]=dayOfStep;
		
		
	       		Message msg = new Message(7028, GlobalData.MESSAGE_ID(),objArr);
	       		
	       		try 
	       		{
	       			GlobalData.client.sendToServer(msg);
	       			while (true)
	       			{
	       				if (GlobalData.client.isRecivedMessage()==true)
	       				{
	       					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
	       				    if (AnswerMessage!=null)
	       				  	      break;
	       				}	
	       			}
	       		} 
	       		catch (IOException e) 
	       		{
	       			// TODO Auto-generated catch block
	       			e.printStackTrace();
	       		}
	       	
	        
		 if (AnswerMessage!=null)
       		 return true;
       	 else 
       		 return false;
		
		


}



	
	public Request[] getAllRequestsWaitingForTimeExtension() {
		
		
		
		
		return null;
		}
	public int createNewRequest() {
		return 0;}  //return requestid
	public void addFileTorequest(){}
	public void addNotesToRequest(){}
	public Request[] getAllRequestsByExaminer(Worker exam) {
		return null;}
	public boolean AddSaffToReuqest(int[] ids) {
		// TODO Auto-generated method stub
		Message msg = new Message(7008, GlobalData.MESSAGE_ID(),ids);
		Message AnswerMessage = null;
		
		try 
		{
			GlobalData.client.sendToServer(msg);
			while (true)
			{
				if (GlobalData.client.isRecivedMessage()==true)
				{
					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
				    if (AnswerMessage!=null)
				  	      break;
				}	
			}
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
		return (Boolean) AnswerMessage.getDataGram();
		
		
	}
	/**
	 * init task  
	 * @param map HashMAp with disgnated params
	 * @return true/false
	 */
	public boolean InitTask(HashMap<String, Integer> map) {
		
		// TODO Auto-generated method stub
		Message msg = new Message(7013, GlobalData.MESSAGE_ID(),map);
		Message AnswerMessage = null;
		
		try 
		{
			GlobalData.client.sendToServer(msg);
			while (true)
			{
				if (GlobalData.client.isRecivedMessage()==true)
				{
					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
				    if (AnswerMessage!=null)
				  	      break;
				}	
			}
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
		return (Boolean) AnswerMessage.getDataGram();
		
		
	}
	
	public List<Request> getAllrequestsByEvaluator(User eval)
	{
		
         if (eval instanceof Worker)
         {
        		Message msg = new Message(7014, GlobalData.MESSAGE_ID(),eval);
        		Message AnswerMessage = null;
        		
        		try 
        		{
        			GlobalData.client.sendToServer(msg);
        			while (true)
        			{
        				if (GlobalData.client.isRecivedMessage()==true)
        				{
        					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
        				    if (AnswerMessage!=null)
        				  	      break;
        				}	
        			}
        		} 
        		catch (IOException e) 
        		{
        			// TODO Auto-generated catch block
        			e.printStackTrace();
        		}
        		System.out.println("AnswerMessage "+ AnswerMessage.getOpCode());
        		if (AnswerMessage.getDataGram() instanceof List<?> && ((( List<?>)AnswerMessage.getDataGram()).isEmpty() == false) && ((( List<?>)AnswerMessage.getDataGram()).get(0) instanceof Request) ) 
        		{
        		return (List<Request>) AnswerMessage.getDataGram();
        		}
        		else
        		{
        			return null;
        		}
        	
         }
		return null;
	}
	public boolean assignExaminer(String string, int reqid) {
		// TODO Auto-generated method stub
		int[] ids = {reqid,Integer.parseInt(string)};
		Message msg = new Message(7016, GlobalData.MESSAGE_ID(),ids);
		Message AnswerMessage = null;
		
		try 
		{
			GlobalData.client.sendToServer(msg);
			while (true)
			{
				if (GlobalData.client.isRecivedMessage()==true)
				{
					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
				    if (AnswerMessage!=null)
				  	      break;
				}	
			}
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (Boolean) AnswerMessage.getDataGram();
	}
																						
																						
																						
												
	@SuppressWarnings("unchecked")
	private List<Request> GenricGetAllRequestByStatusAndPostion(int id,WorkerPostion pos,Status[] stats)
	{
		Message AnswerMessage = null;
		Object[] objarr = new Object[]{new Integer(id),pos,stats};
		Message msg = new Message(7022, GlobalData.MESSAGE_ID(),objarr);
		try 
		{
			GlobalData.client.sendToServer(msg);
			sleep(100);
			while (true)
			{
				if (GlobalData.client.isRecivedMessage()==true)
				{
					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
				    if (AnswerMessage!=null)
				  	      break;
				}	
			}
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(AnswerMessage.getDataGram()!=null && AnswerMessage.getDataGram() instanceof List<?>)
		       return (List<Request>) AnswerMessage.getDataGram();	
		return null;
	}
	
	public List<Request> getAllCloseRequestsByInspector() 
	{
		String mainStatus = RequestMainStatusEnum.CLOSE.getText();
		String inspectorId = GlobalData.worker.getId()+"";
		
		String[] data = new String[]{inspectorId,mainStatus};
		
		Message msg = new Message(7026, GlobalData.MESSAGE_ID(),data);
		Message AnswerMessage = null;
		System.out.println("Enter to getAllCloseRequestsByInspector");
		try 
		{
			GlobalData.client.sendToServer(msg);
			while (true)
			{
				sleep(100);
				if (GlobalData.client.isRecivedMessage()==true)
				{
					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
				    if (AnswerMessage!=null)
				  	      break;
				}	
			}
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		if ( AnswerMessage==null || AnswerMessage.getDataGram() == null)
		{
			// for this user There is not Request to be Close
			return null;
		}
		else
		{
			System.out.println("Return requests from the Data Base");
			if(AnswerMessage.getDataGram() instanceof List<?> )
				   return	(List<Request>)AnswerMessage.getDataGram();
		}
		return null;

	}
	public List<Request> getAllOpenRequestsByInspector() 
	{
		String mainStatus = RequestMainStatusEnum.OPEN.getText();
		String inspectorId = GlobalData.worker.getId()+"";
		
		String[] data = new String[]{inspectorId,mainStatus};
		
		Message msg = new Message(7025, GlobalData.MESSAGE_ID(),data);
		Message AnswerMessage = null;
		System.out.println("Enter to getAllCloseRequestsByInspector");
		try 
		{
			GlobalData.client.sendToServer(msg);
			while (true)
			{
				sleep(100);
				if (GlobalData.client.isRecivedMessage()==true)
				{
					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
				    if (AnswerMessage!=null)
				  	      break;
				}	
			}
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		if ( AnswerMessage==null || AnswerMessage.getDataGram() == null)
		{
			// for this user There is not Request to be Close
			return null;
		}
		else
		{
			System.out.println("Return requests from the Data Base");
			if(AnswerMessage.getDataGram() instanceof List<?> )
				   return	(List<Request>)AnswerMessage.getDataGram();
		}
		return null;

	}
	public List<Request> getAllSuspendedRequestsByInspector() 
	{
		String mainStatus = RequestMainStatusEnum.SUSPEND.getText();
		String inspectorId = GlobalData.worker.getId()+"";
		
		String[] data = new String[]{inspectorId,mainStatus};
		
		Message msg = new Message(7027, GlobalData.MESSAGE_ID(),data);
		Message AnswerMessage = null;
		System.out.println("Enter to getAllCloseRequestsByInspector");
		try 
		{
			GlobalData.client.sendToServer(msg);
			while (true)
			{
				sleep(100);
				if (GlobalData.client.isRecivedMessage()==true)
				{
					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
				    if (AnswerMessage!=null)
				  	      break;
				}	
			}
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		if ( AnswerMessage==null || AnswerMessage.getDataGram() == null)
		{
			// for this user There is not Request to be Close
			return null;
		}
		else
		{
			System.out.println("Return requests from the Data Base");
			if(AnswerMessage.getDataGram() instanceof List<?> )
				   return	(List<Request>)AnswerMessage.getDataGram();
		}
		return null;

	}
	

	public ArrayList<Request> getAllRequestsByStatus(
			Status status) {
		Message AnswerMessage = null;
		
		Message msg = new Message(7024, GlobalData.MESSAGE_ID(),status);
		try 
		{
			GlobalData.client.sendToServer(msg);
			while (true)
			{
				if (GlobalData.client.isRecivedMessage()==true)
				{
					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
				    if (AnswerMessage!=null)
				  	      break;
				}	
			}
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return (ArrayList<Request>) AnswerMessage.getDataGram();
	}
												
			
	public void sleep(long tik)
	{
		try 
		{
			Thread.sleep(tik);
		} 
		catch (InterruptedException e) 
		{
			e.printStackTrace();
		}
	}
												
 
	 public ArrayList<Request> getAllRequestsForExecutionWithoutTime(Worker exeLdr)
	 {
	 	Status stat[] = {(new Status(RequestStepStatusEnum.EXE_UNEXECUTED_WTIHOUT_TIME, RequestMainStatusEnum.OPEN)),
	 		 	(new Status(RequestStepStatusEnum.REV_REVIEWED_PASSED, RequestMainStatusEnum.OPEN)) 
	             

	 };
	 	 return (ArrayList<Request>) (this.GenricGetAllRequestByStatusAndPostion(exeLdr.getId(), WorkerPostion.EXECUATION_LEADER, stat ));
	 }

	
	
	 public ArrayList<Request> getAllRequestsReadyForExecution(Worker exeLdr)
	 {
	 	Status stat[] = {(new Status(RequestStepStatusEnum.EXE_UNEXECUTED_EXTENDED, RequestMainStatusEnum.OPEN)) ,
	             (new Status(RequestStepStatusEnum.EXE_UNEXECUTED_EXTENDED_DENY, RequestMainStatusEnum.OPEN)),
	         	(new Status(RequestStepStatusEnum.EXE_UNEXECUTED_WITH_TIME, RequestMainStatusEnum.OPEN)),
	         	(new Status(RequestStepStatusEnum.EXE_REXECUTE, RequestMainStatusEnum.OPEN))

	 };
	 	 return (ArrayList<Request>) (this.GenricGetAllRequestByStatusAndPostion(exeLdr.getId(), WorkerPostion.EXECUATION_LEADER, stat ));
	 }
	 
public ArrayList<Request> getAllRequestsReadyForEvaluation(Worker evaluator)
{
	Status stat[] = {(new Status(RequestStepStatusEnum.EVL_EVLUATION_EXTENDED, RequestMainStatusEnum.OPEN)) ,
            (new Status(RequestStepStatusEnum.EVL_EVLUATION_EXTENDED_DENY, RequestMainStatusEnum.OPEN)),
        	(new Status(RequestStepStatusEnum.EVL_UNVALUATED_WITH_TIME, RequestMainStatusEnum.OPEN))
};
	               return (ArrayList<Request>) (this.GenricGetAllRequestByStatusAndPostion(evaluator.getId(), WorkerPostion.EVLUATOR, stat ));
}
												
public ArrayList<Request> getAllRequestsReadyForReEvaluation(Worker evaluator)
{
	Status stat[] = {(new Status(RequestStepStatusEnum.EVL_REAVLAUTION_TIME_EXTENSION_DENIED, RequestMainStatusEnum.OPEN)) ,
            (new Status(RequestStepStatusEnum.EVL_REAVLAUTION_TIME_EXTENSION_EXTENDED, RequestMainStatusEnum.OPEN)), 
        	(new Status(RequestStepStatusEnum.EVL_REAVLAUTION_WITH_TIME , RequestMainStatusEnum.OPEN))                    };
	               return (ArrayList<Request>) (this.GenricGetAllRequestByStatusAndPostion(evaluator.getId(), WorkerPostion.EVLUATOR, stat ));
}
public ArrayList<Request> getAllRequestsReadyForEvaluationWithTimePending(Worker evaluator)
{
	Status stat[] = {(new Status(RequestStepStatusEnum.EVL_EVLUATION_TIME_EXTENSION_PENDING, RequestMainStatusEnum.OPEN)) ,
            (new Status(RequestStepStatusEnum.EVL_REAVLAUTION_TIME_EXTENSION_PENDING, RequestMainStatusEnum.OPEN)),
        	(new Status(RequestStepStatusEnum.EVL_UNVALUATED_WITH_TIME_PENDING, RequestMainStatusEnum.OPEN))                    };
	               return (ArrayList<Request>) (this.GenricGetAllRequestByStatusAndPostion(evaluator.getId(), WorkerPostion.EVLUATOR, stat ));
}
public ArrayList<Request> getAllRequestsForEvaluationWithoutTime(Worker evaluator)
{
	Status stat[] = {(new Status(RequestStepStatusEnum.EVL_UNVALUATED_WTIHOUT_TIME, RequestMainStatusEnum.OPEN)) ,
            (new Status(RequestStepStatusEnum.EVL_RETURNED_TO_REVALUTION, RequestMainStatusEnum.OPEN)),
        	(new Status(RequestStepStatusEnum.INILIZED, RequestMainStatusEnum.OPEN))
};
	               return (ArrayList<Request>) (this.GenricGetAllRequestByStatusAndPostion(evaluator.getId(), WorkerPostion.EVLUATOR, stat ));
}


public List<Request> getAllRequestsThatNeedExtantionByInspector() 
{
	Status stat[] = {
					 (new Status(RequestStepStatusEnum.EVL_EVLUATION_TIME_EXTENSION_PENDING   , RequestMainStatusEnum.OPEN  )),
                     (new Status(RequestStepStatusEnum.EVL_REAVLAUTION_TIME_EXTENSION_PENDING    , RequestMainStatusEnum.OPEN  )),
                     (new Status(RequestStepStatusEnum.REV_REVIEWED_TIME_EXTENSION_PENDING    , RequestMainStatusEnum.OPEN  )),
                     (new Status(RequestStepStatusEnum.TST_UTESTED_TIME_EXTENSION_PENDING    , RequestMainStatusEnum.OPEN  )) ,
                     (new Status(RequestStepStatusEnum.EXE_UNEXECUTED_TIME_EXTENSION_PENDING    , RequestMainStatusEnum.OPEN  )) 
                     };

	return (ArrayList<Request>) (this.GenricGetAllRequestByStatusAndPostion(GlobalData.worker.getId(), WorkerPostion.INSPECTOR, stat ));

}
public List<Request> getAllRequestsThatNeedTimeSepApproval() 
{
	Status stat[] = {
			 (new Status(RequestStepStatusEnum.EVL_UNVALUATED_WITH_TIME_PENDING   , RequestMainStatusEnum.OPEN  )),
            (new Status(RequestStepStatusEnum.EXE_UNEXECUTED_WTIH_TIME_PENDING    , RequestMainStatusEnum.OPEN  )),  
            (new Status(RequestStepStatusEnum.EVL_REAVLAUTION_WITH_TIME_PENDING    , RequestMainStatusEnum.OPEN  ))   
            };

		return (ArrayList<Request>) (this.GenricGetAllRequestByStatusAndPostion(GlobalData.worker.getId(), WorkerPostion.INSPECTOR, stat ));

}
												
}
